﻿import com.crystalcentral.model.*;
import com.crystalcentral.view.*;

import mx.utils.Delegate;
import flash.geom.Point;

import com.darronschall.DynamicRegistration;

import flash.filters.BitmapFilter;
import flash.filters.ColorMatrixFilter;
import flash.filters.BevelFilter;

dynamic class com.crystalcentral.view.MItem
{
	// Constants:
	public static var CLASS_REF = com.crystalcentral.view.MItem;
	public static var REFRESH_DURATION = 500;

	// Item Padding & Borders
	var BORDER_COLOR = 0x0070C6;
	var BORDER_WIDTH = 1;
	var PADDING_TOP = 2;
	var PADDING_BOTTOM = 2;
	var PADDING_LEFT = 4;
	var PADDING_RIGHT = 4;

	var mCanvas:MCanvas;

	var diyItem:DiyItem;
	var mcItem:MovieClip;
	var mcContent:MovieClip;
	var mcBorder:MovieClip;

	var mcDelete:MovieClip;
	var mcRotate:MovieClip;
	var mcResize:MovieClip;
	var mcResizeText:MovieClip;

	private var intervalId:Number;
	private var refreshCnt:Number;

	private var lastClickTimer:Number;
	private var isDoubleClick:Boolean;

	private var mcItemDepth:Number;

	private var aUndoDiyItem:Array;
	private var aRedoDiyItem:Array;

	private var lastStatus:DiyItem;

	private var menu:ContextMenu;
	
	

	public function MItem(diyItem:DiyItem, mCanvas:MCanvas)
	{
		// constructor code
		this.diyItem = diyItem;
		this.mCanvas = mCanvas;

		// init MovieClips
		var nDepth = mCanvas.canvas.getNextHighestDepth();
		this.mcItem = mCanvas.canvas.createEmptyMovieClip("mcItem_" + nDepth, nDepth);
		this.mcContent = mcItem.createEmptyMovieClip("mcContent", mcItem.getNextHighestDepth());
		this.mcBorder = mcItem.createEmptyMovieClip("mcBorder", mcItem.getNextHighestDepth());

		this.mcDelete = mcItem.attachMovie("mcDelete", "mcDelete", mcItem.getNextHighestDepth(), {_x:100, _y:0});
		this.mcResize = mcItem.attachMovie("mcResize", "mcResize", mcItem.getNextHighestDepth(), {_x:100, _y:30});
		this.mcRotate = mcItem.attachMovie("mcRotate", "mcRotate", mcItem.getNextHighestDepth(), {_x:50, _y:-20});
		this.mcResizeText = mcItem.attachMovie("mcResizeText", "mcResizeText", mcItem.getNextHighestDepth(), {_x:50, _y:-20});
		this.mcDelete._visible = false;
		this.mcResize._visible = false;
		this.mcRotate._visible = false;
		this.mcResizeText._visible = false;

		DynamicRegistration.initialize(this.mcItem);
		this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);

		// init position, size & rotation;
		this.mcItem._x = diyItem.x;
		this.mcItem._y = diyItem.y;
		this.mcContent._xscale = diyItem.xscale;
		this.mcContent._yscale = diyItem.yscale;
		this.mcItem._rotation = diyItem.rotation;

		// init other variables
		this.lastClickTimer = -1;
		this.isDoubleClick = false;

		// init mouse events;
		initActions();

		// init context menu
		initMenu();
		
		// set colorfill
		setColorfill(_root.diyTool.colorfill);

		_root.addToUndo(this,function (isUndo:Boolean)
		{
		if (isUndo)
		{
		//trace("undo: " + this);
		this.mcItem._visible = false;
		this.mCanvas.setAllLostFocus();
		}
		else
		{
		//trace("redo: " + this);
		this.mcItem._visible = true;
		this.setOnFocus();
		}
		});
		aUndoDiyItem = new Array();
		aRedoDiyItem = new Array();
	}

	function initActions()
	{
		//trace("MItem:initActions()");
		registerClickEvent();
		initEditToolActions();
	}
	function initMenu()
	{
		this.mcItem.menu = _root.diyTool.getContextMenu();
	}
	function registerClickEvent()
	{
		this.mcContent.onPress = Delegate.create(this, onItemPress);
		this.mcContent.onRelease = Delegate.create(this, onItemRelease);
		this.mcContent.onReleaseOutside = Delegate.create(this, onItemReleaseOutside);
	}

	function removeClickEvent()
	{
		//trace(this.mcContent + " removeClickEvent()");
		delete this.mcContent.onPress;
		delete this.mcContent.onRelease;
		delete this.mcContent.onReleaseOutside;
		delete this.mcContent.onEnterFrame;
	}

	function initEditToolActions()
	{
		//trace(this.mcItem + " initEditToolActions()");
		this.mcDelete.onPress = Delegate.create(this, mcDeleteOnPress);
		this.mcDelete.onRelease = Delegate.create(this, mcDeleteOnRelease);

		this.mcRotate.onPress = Delegate.create(this, mcRotateOnPress);
		this.mcRotate.onRelease = Delegate.create(this, mcRotateOnRelease);
		this.mcRotate.onReleaseOutside = Delegate.create(this, mcRotateOnReleaseOutside);

		this.mcResize.onPress = Delegate.create(this, mcResizeOnPress);
		this.mcResize.onRelease = Delegate.create(this, mcResizeOnRelease);
		this.mcResize.onReleaseOutside = Delegate.create(this, mcResizeOnReleaseOutside);

		this.mcResizeText.onPress = Delegate.create(this, mcResizeTextOnPress);
		this.mcResizeText.onRelease = Delegate.create(this, mcResizeTextOnRelease);
		this.mcResizeText.onReleaseOutside = Delegate.create(this, mcResizeTextOnReleaseOutside);
	}

	function begineRefreshItem()
	{
		if (intervalId != null)
		{
			trace("clearInterval");
			clearInterval(intervalId);
		}
		refreshCnt = 0;
		intervalId = setInterval(this, "refreshItem", REFRESH_DURATION);
	}

	function refreshItem()
	{
		//trace("refreshCnt: " + refreshCnt);
		switch (refreshCnt)
		{
			case 0 :
				this.mcItem._rotation += 0.01;
				_root.diyTool.zoomTo(_root.diyTool.diyInfo.zoomScale + 0.1);
				refreshCnt++;
				break;
			case 1 :
				this.mcItem._rotation -= 0.01;
				_root.diyTool.zoomTo(_root.diyTool.diyInfo.zoomScale - 0.1);
				refreshCnt++;
				break;
			case 2 :
				clearInterval(intervalId);
				refreshCnt = 0;
				this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);

				this.setOnFocus();
				break;
			default :

		}
	}
	public function set x(_x:Number)
	{
		this.mcItem._x = _x;
		this.diyItem.x = _x;
	}
	public function get x()
	{
		return this.mcItem._x;
	}
	public function set y(_y:Number)
	{
		this.mcItem._y = _y;
		this.diyItem.y = _y;
	}
	public function get y()
	{
		return this.mcItem._y;
	}
	public function set width(_width:Number)
	{
		this.mcContent._width = _width;
		this.diyItem.width = _width;
	}
	public function get width()
	{
		return this.mcContent._width;
	}
	public function set height(_height:Number)
	{
		this.mcContent._height = _height;
		this.diyItem.height = _height;
	}
	public function get height()
	{
		return this.mcContent._height;
	}
	public function set xscale(_xscale:Number)
	{
		this.mcContent._xscale = _xscale;
		this.diyItem.xscale = _xscale;
	}
	public function get xscale()
	{
		return this.mcContent._xscale;
	}
	public function set yscale(_yscale:Number)
	{
		this.mcContent._yscale = _yscale;
		this.diyItem.yscale = _yscale;
	}
	public function get yscale()
	{
		return this.mcContent._yscale;
	}
	public function set rotation(_rotation:Number)
	{
		this.mcItem._rotation = _rotation;
		this.diyItem.rotation = _rotation;
	}
	public function get rotation()
	{
		return this.mcItem._rotation;
	}

	public function get type()
	{
		return this.diyItem.type;
	}

	function setOnFocus()
	{
		mCanvas.setAllLostFocus(this);
		//trace(this.mcItem + " setOnFocus()");
		mCanvas.currentItem = this;
		//trace(mCanvas.currentItem.mcItem);
		showBorder();
		showEditTools();
		if (this.mcItemDepth == undefined || this.mcItemDepth == -1)
		{
			this.mcItemDepth = this.mcItem.getDepth();
		}
		//this.mcItem.swapDepths(_root.getNextHighestDepth()); 
	}
	function setLostFocus()
	{
		//trace(this.mcItem + " setLostFocus()");
		hideBorder();
		hideEditTools();
		if (this.mcItemDepth != undefined && this.mcItemDepth != -1)
		{
			//this.mcItem.swapDepths(this.mcItemDepth);
			this.mcItemDepth = -1;
		}
	}

	function drawBorder()
	{
		this.mcBorder.clear();

		var bWidth = BORDER_WIDTH;
		//bWidth = Math.floor(bWidth + 0.5);
		var xl = 0 - PADDING_LEFT;
		var xr = this.width + PADDING_RIGHT;
		var yt = 0 - PADDING_TOP;
		var yb = this.height + PADDING_BOTTOM;

		this.mcBorder._visible = true;
		this.mcBorder.clear();
		this.mcBorder.lineStyle(bWidth,BORDER_COLOR);
		this.mcBorder.moveTo(xl,yt);
		this.mcBorder.lineTo(xl,yb);
		this.mcBorder.lineTo(xr,yb);
		this.mcBorder.lineTo(xr,yt);
		this.mcBorder.lineTo(xl,yt);

		/*this.mcBorder.lineStyle(1,0xFFFFFF);
		this.mcBorder.moveTo(this.width / 2 - 10, this.height / 2);
		this.mcBorder.lineTo(this.width / 2 + 10, this.height / 2);
		this.mcBorder.moveTo(this.width / 2, this.height / 2 - 10);
		this.mcBorder.lineTo(this.width / 2, this.height / 2 + 10);*/
	}

	function showBorder()
	{
		//trace("MItem:showBorder()");
		this.mcBorder._visible = true;
		drawBorder();
	}

	function hideBorder()
	{
		this.mcBorder._visible = false;
	}

	function showEditTools()
	{
		this.mcDelete._visible = true;
		this.mcRotate._visible = true;
		this.mcResize._visible = true;
		this.mcResizeText._visible = true;

		this.mcDelete._x = this.width + PADDING_RIGHT;
		this.mcDelete._y = 0 - this.mcDelete._height - PADDING_TOP;

		//this.mcRotate._x = this.tf._width / 2 - this.mcRotate._width / 2;
		this.mcRotate._x = 0 - 23 - PADDING_LEFT;
		this.mcRotate._y = 0 - 23 - PADDING_TOP;

		this.mcResize._x = this.width + PADDING_RIGHT;
		this.mcResize._y = this.height + PADDING_BOTTOM;

		this.mcResizeText._x = this.width + PADDING_RIGHT;
		this.mcResizeText._y = this.height / 2 - this.mcResizeText._height / 2;
	}
	function hideEditTools()
	{
		this.mcDelete._visible = false;
		this.mcRotate._visible = false;
		this.mcResize._visible = false;
		this.mcResizeText._visible = false;
	}

	function remove()
	{
		this.mcItem._visible = false;

		_root.addToUndo(this,function (isUndo:Boolean)
		{
		if (isUndo)
		{
		this.mcItem._visible = true;
		}
		else
		{
		this.mcItem._visible = false;
		}
		});
	}

	function moveTo(xNew:Number, yNew:Number)
	{
		trace("MItem:moveTo()");
		if (this.diyItem.type == "text" && this.editable) {
			return;
		}
		
		this.lastStatus = new DiyItem();
		this.lastStatus.x = this.diyItem.x;
		this.lastStatus.y = this.diyItem.y;

		this.aUndoDiyItem.push(this.lastStatus);
		
		this.diyItem.x = xNew;
		this.diyItem.y = yNew;
		this.mcItem._x = xNew;
		this.mcItem._y = yNew;

		_root.addToUndo(this,function (isUndo:Boolean)
		{
		if (isUndo)
		{
		var undoDiyItem = this.aUndoDiyItem.pop();
		this.aRedoDiyItem.push(this.diyItem.clone());
		this.diyItem.x = undoDiyItem.x;
		this.diyItem.y = undoDiyItem.y;
		this.mcItem._x = this.diyItem.x;
		this.mcItem._y = this.diyItem.y;
		}
		else
		{
		var redoDiyItem = this.aRedoDiyItem.pop();
		this.aUndoDiyItem.push(this.diyItem.clone());
		this.diyItem.x = redoDiyItem.x;
		this.diyItem.y = redoDiyItem.y;
		this.mcItem._x = this.diyItem.x;
		this.mcItem._y = this.diyItem.y;
		}
		});
	}

	function onItemPress()
	{
		trace(this.mcContent + " onPress()");
		//trace("this.mcItem(" + this.mcItem._xmouse + "," + this.mcItem._ymouse + ")");

		//if (checkToolActions("press")) return;

		if (this.lastClickTimer != -1)
		{
			if (getTimer() / 1000 - this.lastClickTimer <= 0.75)
			{
				this.isDoubleClick = true;
			}
		}
		this.setOnFocus();
		//this.mcItem.swapDepths(mCanvas.canvas.getNextHighestDepth());
		this.mcContent.startDrag();
		this.mcContent.onEnterFrame = Delegate.create(this, onItemEnterFrame);

		this.lastClickTimer = getTimer() / 1000;

		this.lastStatus = new DiyItem();
		this.lastStatus.x = this.diyItem.x;
		this.lastStatus.y = this.diyItem.y;
	}

	function onItemRelease()
	{
		trace(this.mcContent + " onItemRelease()");
		//checkToolActions("release");

		this.mcContent.stopDrag();
		delete this.mcContent.onEnterFrame;
		//setOnFocus();
		this.diyItem.x = this.mcItem._x;
		this.diyItem.y = this.mcItem._y;

		//trace(Math.abs(this.diyItem.x - this.lastStatus.x));
		//trace(Math.abs(this.diyItem.y - this.lastStatus.y));

		if (this.lastStatus != undefined && (Math.abs(this.diyItem.x - this.lastStatus.x) > 1 || Math.abs(this.diyItem.y - this.lastStatus.y) > 1))
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.x = undoDiyItem.x;
			this.diyItem.y = undoDiyItem.y;
			this.mcItem._x = this.diyItem.x;
			this.mcItem._y = this.diyItem.y;
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.x = redoDiyItem.x;
			this.diyItem.y = redoDiyItem.y;
			this.mcItem._x = this.diyItem.x;
			this.mcItem._y = this.diyItem.y;
			}
			});
		}
		this.lastStatus = undefined;

		if (this.isDoubleClick)
		{
			onItemDoubleClick();
		}
		this.isDoubleClick = false;
	}

	function onItemReleaseOutside()
	{
		trace(this.mcContent + " onItemReleaseOutside()");
		//checkToolActions("releaseOutside");

		this.mcContent.stopDrag();
		delete this.mcContent.onEnterFrame;
		this.diyItem.x = this.mcItem._x;
		this.diyItem.y = this.mcItem._y;

		this.isDoubleClick = false;

		if (this.lastStatus != undefined && (Math.abs(this.diyItem.x - this.lastStatus.x) > 1 || Math.abs(this.diyItem.y - this.lastStatus.y) > 1))
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.x = undoDiyItem.x;
			this.diyItem.y = undoDiyItem.y;
			this.mcItem._x = this.diyItem.x;
			this.mcItem._y = this.diyItem.y;
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.x = redoDiyItem.x;
			this.diyItem.y = redoDiyItem.y;
			this.mcItem._x = this.diyItem.x;
			this.mcItem._y = this.diyItem.y;
			}
			});
		}
		this.lastStatus = undefined;
	}

	function onItemDoubleClick()
	{
		trace(this.mcContent + " onItemDoubleClick()");
	}

	function onItemEnterFrame()
	{
		var pos:Point = new Point(this.mcContent._x, this.mcContent._y);
		this.mcItem.localToGlobal(pos);
		this.mCanvas.canvas.globalToLocal(pos);

		this.mcItem._x = pos.x;
		this.mcItem._y = pos.y;

		this.mcContent._x = 0;
		this.mcContent._y = 0;

		this.diyItem.x = this.mcItem._x;
		this.diyItem.y = this.mcItem._y;

		this.showBorder();
		this.showEditTools();
	}

	// mcDelete Actions
	function mcDeleteOnPress()
	{
		trace("MItem:mcDeleteOnPress()");
	}

	function mcDeleteOnRelease()
	{
		trace("MItem:mcDeleteOnRelease()");
		this.remove();
	}

	// mcRotate Actions
	function mcRotateOnPress()
	{
		trace("MItem:mcRotateOnPress()");
		this.mcRotate.startDrag();
		this.mcRotate.onEnterFrame = Delegate.create(this, mcRotateOnEnterFrame);
		Mouse.hide();

		this.lastStatus = new DiyItem();
		this.lastStatus.rotation = this.diyItem.rotation;
	}
	function mcRotateOnEnterFrame()
	{
		var pos:Point = new Point(this.mcRotate._x + this.mcRotate.btnRotate._width, this.mcRotate._y + this.mcRotate.btnRotate._height);
		var center:Point = new Point(this.mcContent._width / 2, this.mcContent._height / 2);

		var alpha:Number = Math.atan2(pos.y - center.y, pos.x - center.x);
		var beta:Number = Math.atan2(-PADDING_TOP - center.y, -PADDING_LEFT - center.x);

		var rotateDegree:Number = (alpha - beta) * 180 / Math.PI;
		//rotateDegree = Math.round(rotateDegree/5) * 5;
		rotateDegree = Math.round(rotateDegree);
		this.mcItem._rotation2 += rotateDegree;
		this.diyItem.rotation = this.mcItem._rotation2;

		this.mcRotate.txtDegree.text = this.diyItem.rotation + "°";

		this.showBorder();
		this.showEditTools();
	}
	function mcRotateOnRelease()
	{
		trace("MItem:mcRotateOnRelease()");
		this.mcRotate.stopDrag();
		delete this.mcRotate.onEnterFrame;

		var rotateDegree:Number = this.diyItem.rotation;
		rotateDegree = Math.round(rotateDegree / 15) * 15;
		this.mcItem._rotation2 = rotateDegree;
		this.diyItem.rotation = this.mcItem._rotation2;
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;
		this.diyItem.x = this.mcItem._x;
		this.diyItem.y = this.mcItem._y;

		this.mcRotate.txtDegree.text = "";
		Mouse.show();
		this.setOnFocus();

		if (this.lastStatus != undefined && Math.abs(this.diyItem.rotation - this.lastStatus.rotation) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.rotation = undoDiyItem.rotation;
			this.mcItem._rotation2 = this.diyItem.rotation;
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.rotation = redoDiyItem.rotation;
			this.mcItem._rotation2 = this.diyItem.rotation;
			}
			});
		}
		this.lastStatus = undefined;
	}
	function mcRotateOnReleaseOutside()
	{
		trace("MItem:mcRotateOnReleaseOutside()");
		this.mcRotate.stopDrag();
		delete this.mcRotate.onEnterFrame;

		var rotateDegree:Number = this.diyItem.rotation;
		rotateDegree = Math.round(rotateDegree / 5) * 5;
		this.mcItem._rotation2 = rotateDegree;
		this.diyItem.rotation = this.mcItem._rotation2;
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;
		this.diyItem.x = this.mcItem._x;
		this.diyItem.y = this.mcItem._y;

		this.mcRotate.txtDegree.text = "";
		Mouse.show();
		this.setOnFocus();

		if (this.lastStatus != undefined && Math.abs(this.diyItem.rotation - this.lastStatus.rotation) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.rotation = undoDiyItem.rotation;
			this.mcItem._rotation2 = this.diyItem.rotation;
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.rotation = redoDiyItem.rotation;
			this.mcItem._rotation2 = this.diyItem.rotation;
			}
			});
		}
		this.lastStatus = undefined;
	}

	// mcResize Actions
	function mcResizeOnPress()
	{
		trace("MItem:mcResizeOnPress()");
		this.mcResize.startDrag();
		this.mcResize.onEnterFrame = Delegate.create(this, mcResizeOnEnterFrame);
		Mouse.hide();

		this.lastStatus = new DiyItem();
		this.lastStatus.xscale = this.diyItem.xscale;
		this.lastStatus.yscale = this.diyItem.yscale;
	}
	function mcResizeOnEnterFrame()
	{
		var pos:Point = new Point(this.mcResize._x, this.mcResize._y);

		if ((pos.y - PADDING_BOTTOM) < 2)
		{
			pos.y = 2 + PADDING_BOTTOM;
		}
		if ((pos.x - PADDING_RIGHT) < 2)
		{
			pos.x = 2 + PADDING_RIGHT;
		}

		var ratioY = (pos.y - PADDING_BOTTOM) / (this.mcContent._height);
		var ratioX = (pos.x - PADDING_RIGHT) / (this.mcContent._width);
		var ratio = Math.max(ratioX, ratioY);
		if (rationX + ratioY < 2) ratio = Math.min(ratioX, ratioY);

		this.mcContent._xscale *= ratio;
		this.mcContent._yscale *= ratio;
		this.diyItem.xscale = this.mcContent._xscale;
		this.diyItem.yscale = this.mcContent._yscale;

		this.showBorder();
		this.showEditTools();
	}
	function mcResizeOnRelease()
	{
		trace("MItem:mcResizeOnRelease()");
		this.mcResize.stopDrag();
		delete this.mcResize.onEnterFrame;
		Mouse.show();
		this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
		this.setOnFocus();
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;

		if (this.lastStatus != undefined && Math.abs(this.diyItem.xscale - this.lastStatus.xscale) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.xscale = undoDiyItem.xscale;
			this.diyItem.yscale = undoDiyItem.yscale;
			this.mcContent._xscale = this.diyItem.xscale;
			this.mcContent._yscale = this.diyItem.yscale;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.xscale = redoDiyItem.xscale;
			this.diyItem.yscale = redoDiyItem.yscale;
			this.mcContent._xscale = this.diyItem.xscale;
			this.mcContent._yscale = this.diyItem.yscale;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			});
		}
		this.lastStatus = undefined;
	}
	function mcResizeOnReleaseOutside()
	{
		trace("MItem:mcResizeOnReleaseOutside()");
		this.mcResize.stopDrag();
		delete this.mcResize.onEnterFrame;
		Mouse.show();
		this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
		this.setOnFocus();
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;

		if (this.lastStatus != undefined && Math.abs(this.diyItem.xscale - this.lastStatus.xscale) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyItem.clone());
			this.diyItem.xscale = undoDiyItem.xscale;
			this.diyItem.yscale = undoDiyItem.yscale;
			this.mcContent._xscale = this.diyItem.xscale;
			this.mcContent._yscale = this.diyItem.yscale;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyItem.clone());
			this.diyItem.xscale = redoDiyItem.xscale;
			this.diyItem.yscale = redoDiyItem.yscale;
			this.mcContent._xscale = this.diyItem.xscale;
			this.mcContent._yscale = this.diyItem.yscale;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			});
		}
		this.lastStatus = undefined;
	}

	// mcResizeText Actions
	function mcResizeTextOnPress()
	{
		trace("MItem:mcResizeTextOnPress()");
		this.mcResizeText.startDrag();
		this.mcResizeText.onEnterFrame = Delegate.create(this, mcResizeTextOnEnterFrame);
		Mouse.hide();

		this.lastStatus = new DiyItem();
		this.lastStatus.width = this.tf._width;
	}
	function mcResizeTextOnEnterFrame()
	{
		var pos:Point = new Point(this.mcResizeText._x, this.mcResizeText._y);

		if ((pos.x - PADDING_RIGHT) < 20)
		{
			pos.x = 20 + PADDING_RIGHT;
		}

		this.tf.wordWrap = true;
		this.tf._width = (pos.x - PADDING_RIGHT) / this.mcContent._xscale * 100;
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;

		this.showBorder();
		this.showEditTools();
	}
	function mcResizeTextOnRelease()
	{
		trace("MItem:mcResizeTextOnRelease()");
		this.mcResizeText.stopDrag();
		delete this.mcResizeText.onEnterFrame;
		Mouse.show();
		this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
		this.setOnFocus();
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;

		if (this.lastStatus != undefined && Math.abs(this.tf._width - this.lastStatus.width) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyTextItem.clone());
			this.tf._width = undoDiyItem.width;
			this.diyItem.width = this.mcContent._width;
			this.diyItem.height = this.mcContent._height;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyTextItem.clone());
			this.tf._width = redoDiyItem.width;
			this.diyItem.width = this.mcContent._width;
			this.diyItem.height = this.mcContent._height;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			});
		}
		this.lastStatus = undefined;
	}
	function mcResizeTextOnReleaseOutside()
	{
		trace("MItem:mcResizeTextOnReleaseOutside()");
		this.mcResizeText.stopDrag();
		delete this.mcResizeText.onEnterFrame;
		Mouse.show();
		this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
		this.setOnFocus();
		this.diyItem.width = this.mcContent._width;
		this.diyItem.height = this.mcContent._height;

		if (this.lastStatus != undefined && Math.abs(this.tf._width - this.lastStatus.width) > 1)
		{
			this.aUndoDiyItem.push(this.lastStatus);

			_root.addToUndo(this,function (isUndo:Boolean)
			{
			if (isUndo)
			{
			var undoDiyItem = this.aUndoDiyItem.pop();
			this.aRedoDiyItem.push(this.diyTextItem.clone());
			this.tf._width = undoDiyItem.width;
			this.diyItem.width = this.mcContent._width;
			this.diyItem.height = this.mcContent._height;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			else
			{
			var redoDiyItem = this.aRedoDiyItem.pop();
			this.aUndoDiyItem.push(this.diyTextItem.clone());
			this.tf._width = redoDiyItem.width;
			this.diyItem.width = this.mcContent._width;
			this.diyItem.height = this.mcContent._height;
			this.mcItem.setRegistration(this.mcContent._width / 2,this.mcContent._height / 2);
			this.setOnFocus();
			}
			});
		}
		this.lastStatus = undefined;
	}

	// deprecated
	function checkToolActions(type:String)
	{
		switch (type)
		{
			case "press" :
				return checkToolPress();
				break;
			case "release" :
				return checkToolRelease();
				break;
			case "releaseOutside" :
				return checkToolReleaseOutside();
				break;
			default :
				return false;
		}
	}
	// deprecated
	function checkToolPress()
	{
		var point:Point = new Point(this.mcItem._xmouse, this.mcItem._ymouse);
		this.mcItem.localToGlobal(point);

		var mcTools:Array = new Array(this.mcDelete, this.mcRotate, this.mcResize, this.mcResizeText);
		for (var i = 0; i < mcTools.length; i++)
		{
			if (mcTools[i].hitTest(point.x, point.y))
			{
				trace(mcTools[i] + " pressed");
				mcTools[i].onPress();
				mcTools[i].pressed = true;
				return true;
			}
		}
		return false;
	}
	// deprecated
	function checkToolRelease()
	{
		var point:Point = new Point(this.mcItem._xmouse, this.mcItem._ymouse);
		this.mcItem.localToGlobal(point);

		var mcTools:Array = new Array(this.mcDelete, this.mcRotate, this.mcResize, this.mcResizeText);

		for (var i = 0; i < mcTools.length; i++)
		{
			if (mcTools[i].pressed)
			{
				if (mcTools[i].hitTest(point.x, point.y))
				{
					mcTools[i].onRelease();
					mcTools[i].pressed = false;
					return true;
				}
				else
				{
					mcTools[i].onReleaseOutside();
					mcTools[i].pressed = false;
					return true;
				}
			}
			mcTools[i].pressed = false;
		}

		return false;
	}
	// deprecated
	function checkToolReleaseOutside()
	{
		return false;
	}

	function bringToFront()
	{
		trace("bringToFront(" + this.mcItem + ")");
		var iLen = mCanvas.mItems.length;
		if (iLen < 2)
		{
			return;
		}

		var aLayerDepth = new Array();
		for (var i = 0; i < iLen; i++)
		{
			aLayerDepth[i] = {layer:Object, depth:Number};
			aLayerDepth[i].layer = mCanvas.mItems[i].mcItem;
			aLayerDepth[i].depth = mCanvas.mItems[i].mcItem.getDepth();
		}
		aLayerDepth.sortOn("depth");

		var currentItemIndex = 0;
		for (var i = 0; i < iLen; i++)
		{
			if (aLayerDepth[i].layer == mCanvas.currentItem.mcItem)
			{
				currentItemIndex = i;
				break;
			}
		}
		for (var i = currentItemIndex + 1; i < iLen; i++)
		{
			mCanvas.currentItem.mcItem.swapDepths(aLayerDepth[i].layer);
		}

		//mCanvas.currentItem.mcItem.swapDepths(aLayerDepth[iLen-1].depth + 1);
	}
	function sendToBack()
	{
		trace("sendToBack(" + this.mcItem + ")");
		var iLen = mCanvas.mItems.length;
		if (iLen < 2)
		{
			return;
		}

		var aLayerDepth = new Array();
		for (var i = 0; i < iLen; i++)
		{
			aLayerDepth[i] = {layer:Object, depth:Number};
			aLayerDepth[i].layer = mCanvas.mItems[i].mcItem;
			aLayerDepth[i].depth = mCanvas.mItems[i].mcItem.getDepth();
		}
		aLayerDepth.sortOn("depth");

		var currentItemIndex = 0;
		for (var i = 0; i < iLen; i++)
		{
			if (aLayerDepth[i].layer == mCanvas.currentItem.mcItem)
			{
				currentItemIndex = i;
				break;
			}
		}
		for (var i = currentItemIndex - 1; i >= 0; i--)
		{
			mCanvas.currentItem.mcItem.swapDepths(aLayerDepth[i].layer);
		}

	}
	
	function setColorfill(isColorfill:Boolean) {
		if (isColorfill) {
			//mcContent.filters = new Array(_root.diyTool.bevelFilter);
		}
		else {
			//mcContent.filters = new Array(_root.diyTool.noColorfillFilter, _root.diyTool.bevelFilter);
		}
	}
}